<?php

declare(strict_types=1);

namespace app\admin\controller;

use think\Request;
use app\admin\controller\Base;
use app\admin\service\SystemConfig as SystemConfigService;
use app\admin\validate\SystemConfig as SystemConfigValidate;

class SystemConfig extends Base
{

    protected $request;
    protected $service;
    protected $validate;
    protected $operator_id;

    public function __construct(Request $request, SystemConfigService $systemConfigService, SystemConfigValidate $systemConfigValidate)
    {
        $this->request = $request;
        $this->service = $systemConfigService;
        $this->validate = $systemConfigValidate;
        $this->operator_id = $request->operator_id ?? null; // Default to null if not set
    }


    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $params = $this->request->params;

        if (!$this->validate->scene('search')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }

        $intPage =  $this->request->param('pageIndex', 1);
        $intPageSize =  $this->request->param('pageSize', 50);
        $res = $this->service->getList([], 'id,name,key,value,creator,create_time',(int)$intPage, (int)$intPageSize);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $params = $this->request->params;

        if (!$this->validate->scene('save')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $params['key'] =  strtoupper($params['key']);
        $params['creator'] = $this->operator_id;
        $params['create_time'] = date('Y-m-d H:i:s', time());

        $res = $this->service->save($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id)
    {
        if ($id <= 0) {
            return $this->returnJsonData(100001, [], '参数有误');
        }
        $params = $this->request->params;
        if (!$this->validate->scene('update')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $params['key'] =  strtoupper($params['key']);
        $condition = [
            'id' => $id,
        ];

        unset($params['id']);
        $res = $this->service->update($condition, $params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if ($id <= 0) {
            return $this->returnJsonData(100001, [], '参数有误');
        }
        $params = $this->request->params;
        if (!$this->validate->scene('update')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }


        $condition = [
            'id' => $id,
        ];
        $params['is_del'] = time();
        $res = $this->service->update($condition, $params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
}
